<?php
/**
 * 报单币
 * * @网店运维 (c) 2015-2018 ShopWWI Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权
 */
defined('ByShopWWI') or exit('Access Invalid!');
class coinModel extends Model {
    /**
     * 生成充值编号
     * @return string
     */
    public function makeSn() {
       return mt_rand(10,99)
              . sprintf('%010d',time() - 946656000)
              . sprintf('%03d', (float) microtime() * 1000)
              . sprintf('%03d', (int) $_SESSION['member_id'] % 1000);
    }

   

    /**
     * 取得充值列表
     * @param unknown $condition
     * @param string $pagesize
     * @param string $fields
     * @param string $order
     */
    public function getCoinRechargeList($condition = array(), $pagesize = '', $fields = '*', $order = '', $limit = '') {
        return $this->table('coin_recharge')->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize)->select();
    }

    /**
     * 添加充值记录
     * @param array $data
     */
    public function addCoinRecharge($data) {
        return $this->table('coin_recharge')->insert($data);
    }

    /**
     * 编辑
     * @param unknown $data
     * @param unknown $condition
     */
    public function editCoinRecharge($data,$condition = array()) {
        return $this->table('coin_recharge')->where($condition)->update($data);
    }

    /**
     * 取得单条充值信息
     * @param unknown $condition
     * @param string $fields
     */
    public function getCoinRechargeInfo($condition = array(), $fields = '*') {
        return $this->table('coin_recharge')->where($condition)->field($fields)->find();
    }

    /**
     * 取充值信息总数
     * @param unknown $condition
     */
    public function getCoinRechargeCount($condition = array()) {
        return $this->table('coin_recharge')->where($condition)->count();
    }

    

    /**
     * 取日志总数
     * @param unknown $condition
     */
    public function getCoinRecordCount($condition = array()) {
        return $this->table('coin_record')->where($condition)->count();
    }

    /**
     * 取得金币变更日志列表
     * @param unknown $condition
     * @param string $pagesize
     * @param string $fields
     * @param string $order
     */
    public function getCoinRecordList($condition = array(), $pagesize = '', $fields = '*', $order = '', $limit = '') {
        return $this->table('coin_record')->where($condition)->field($fields)->order($order)->limit($limit)->page($pagesize)->select();
    }

   

    /**
     * 变更报单币
     * @param unknown $change_type
     * @param unknown $data
     * @throws Exception
     * @return unknown
     */
     public function changeCoin($change_type,$data = array()) {
        $data_log = array();
        $data_pd = array();
        $data_msg = array();
		
        $data_log['lg_invite_member_id'] = $data['invite_member_id'];
        $data_log['lg_member_id'] = $data['member_id'];
        $data_log['lg_member_name'] = $data['member_name'];
        $data_log['lg_add_time'] = TIMESTAMP;
        $data_log['lg_type'] = $change_type;

        $data_msg['time'] = date('Y-m-d H:i:s');
        $data_msg['pd_url'] = urlMember('predeposit', 'pd_log_list');
        switch ($change_type){
            case 'order_pay':
                $data_log['lg_av_amount'] = -$data['amount'];
                $data_log['lg_desc'] = '下单，支付预存款，订单号: '.$data['order_sn'];
                $data_pd['submit_coin'] = array('exp','submit_coin-'.$data['amount']);

                break;
            case 'order_freeze':
                $data_log['lg_av_amount'] = -$data['amount'];
                $data_log['lg_freeze_amount'] = $data['amount'];
                $data_log['lg_desc'] = '下单，冻结预存款，订单号: '.$data['order_sn'];
                $data_pd['freeze_predeposit'] = array('exp','freeze_predeposit+'.$data['amount']);
                $data_pd['submit_coin'] = array('exp','submit_coin-'.$data['amount']);

                break;
            case 'order_cancel':
                $data_log['lg_av_amount'] = $data['amount'];
                $data_log['lg_freeze_amount'] = -$data['amount'];
                $data_log['lg_desc'] = '取消订单，解冻预存款，订单号: '.$data['order_sn'];
                $data_pd['freeze_predeposit'] = array('exp','freeze_predeposit-'.$data['amount']);
                $data_pd['submit_coin'] = array('exp','submit_coin+'.$data['amount']);

               
                break;
            case 'order_comb_pay':
                $data_log['lg_freeze_amount'] = -$data['amount'];
                $data_log['lg_desc'] = '下单，支付被冻结的预存款，订单号: '.$data['order_sn'];
                $data_pd['freeze_predeposit'] = array('exp','freeze_predeposit-'.$data['amount']);

                break;
	   
            case 'recharge':
                $data_log['lg_av_amount'] = $data['amount'];
                $data_log['lg_desc'] = '充值，充值单号: '.$data['pdr_sn'];
                $data_log['lg_admin_name'] = $data['admin_name'];
                $data_pd['submit_coin'] = array('exp','submit_coin+'.$data['amount']);

                break;
            case 'register':
                $data_log['lg_av_amount'] = $data['amount'];
                $data_log['lg_desc'] = '注册会员：'. $data['register_member_type'].$data['register_member_name'].',消耗报单币：'.$data['amount'].'个';
                $data_log['lg_admin_name'] = $data['admin_name'];
                $data_pd['submit_coin'] = array('exp','submit_coin+'.$data['amount']);

                break;

            case 'refund':
                $data_log['lg_av_amount'] = $data['amount'];
                $data_log['lg_desc'] = '确认退款，订单号: '.$data['order_sn'];
                $data_pd['submit_coin'] = array('exp','submit_coin+'.$data['amount']);

                break;
          
       
            
            
            default:
                throw new Exception('参数错误');
                break;
        }
        
        $update = Model('member')->editMember(array('member_id'=>$data['member_id']),$data_pd);

        if (!$update) {
            throw new Exception('操作失败');
        }
        $insert = $this->table('coin_record')->insert($data_log);
        if (!$insert) {
            throw new Exception('操作失败');
        }

    
        return $insert;
    }

    /**
     * 删除充值记录
     * @param unknown $condition
     */
    public function delCoinRecharge($condition) {
        return $this->table('coin_recharge')->where($condition)->delete();
    }

    public function writeLog($member_id,$amount,$change_type,$register_member_id,$register_member_name,$register_member_type)
    {
         $model_member=Model('member');
         $data = array();
            
         $member=$model_member->getMemberInfo(array('member_id'=>$member_id));
         $data['member_id'] = $member_id;
         $data['member_name'] =empty($member)?'': $member['member_name'];
         $data['amount'] = $amount;//奖励金额
         
         $data['register_member_id'] = $register_member_id;
         $data['register_member_name'] = $register_member_name;
         $data['register_member_type'] =$model_member->getMemberTypeName($register_member_type) ;
        
         
         $this->changeCoin($change_type,$data);
        
    }
    

}
